import os
import sys

#Parse content of input file
#content = open("in", "r").read()

#is_id = True
dic = {}
#for line in content.split("\n"):
#   if is_id:
#        is_id=False
#        if line.strip() != "":
#            job_id = int(line.strip())
#   else:
#       is_id = True
#       line = line.replace("'", "")
#       experiment = line.strip().split(" ")
#       experiment = tuple(experiment)
#       dic[experiment] = job_id 
   
for filename in os.listdir("./"):
    if os.path.isfile("./" + filename):
        content = filename.replace("tmp-", "").replace(".program","").replace(".results","").replace(".tgz","").replace("_job","")
        content = content.split("-")
        id = content[1].strip()
        exp = content[0].split("_")[0].strip()
        name = content[0].strip()
        dic[tuple([exp, name])] = id
print dic               
# Experiments order
exp_order = ['mmhc', 'GS', 'bbsl']
net_order = ['alarm', 'hailfinder', 'child', 'insurance']
example_order = ['500', '1000']
perc_order = ['30','15','5','1']
init_structure_order = ['zero', 'net1', 'net2', 'net3', 'net4', 'net5']

out = open("./out.csv", "a")
for exp in exp_order:
    for net in net_order:
        for example in example_order:
            for perc in perc_order:
                for init_structure in init_structure_order:
                    name = exp + "_" + net.lower() + "_" + example + "_perc" + perc + "_" + init_structure
                    #print name
                    if (exp, name) in dic:
                        job_id = dic[(exp, name)]
                        # Check if the tgz file exist
                        filename = "tmp-" + name + "_" + "job-" + str(job_id) + ".results.tgz"                        
                        if os.path.exists("./" + filename):
                            os.system("tar -zxf " + filename)
                            if os.path.exists("results/prog.out"):
                                result_content = open("results/prog.out", "r").read()
                                os.system("rm -r results")
                            else:
                                root_dir = os.getcwd()
                                os.chdir("tmp")
                                for i in range(1, 6):
                                    dir = os.listdir(".")[0]
                                    os.chdir(dir)                                
                                result_content = open("prog.out", "r").read()
                                os.chdir(root_dir)
                                os.system("rm -r tmp")
                            
                            # result content presentation
                            result_content = result_content.replace("(", "").replace(")","").replace("Decimal", "").replace("'", "")
                            result_content_final = ""
                            for line in result_content.split("\n"):
                                if line.strip() != "":
                                    result_content_final += ", ".join([exp, net, example, perc, init_structure]) + ", " + line + "\n" 
                            ######
                            
                            out.write(result_content_final)
                        else:
                            #out.write("Not Available\n" * 20)
                            print "Job ("+ exp + "," + name + ") : file doesn't exist"
                            
                    else:
                        # Job not in list
                        #out.write("Not Available\n" * 20)
                        #print "Job ("+ exp + "," + name + ") not in list"
                        pass
out.close()   
